List getting corrupted

4 threads are runing this function processthread and i am taking a lock while adding at g_cProcessMutex,but sometimes list.pop_back() gives SIGABORT,what would u suggest
<code>
void* ProcessThread(void* pArg)
{
int lnReturnVal = 0;
while(true)
{
if(!g_cProcessList.empty())
{
//pthread_mutex_unlock(&g_cProcessMutex);
printf("In funtion \nthread id = %d\n", pthread_self());
lnReturnVal = pthread_mutex_lock(&g_cProcessMutex);
if(lnReturnVal != 0)
{
printf("%s, %d ",strerror(errno),__LINE__);
perror("unable to take lock");
exit(1);
}
cout << "process thread process mutex acquired" << endl;
tagData lstData = g_cProcessList.back();
g_cProcessList.pop_back();

lnReturnVal = pthread_mutex_unlock(&g_cProcessMutex);
if(lnReturnVal != 0)
{
printf("%s, %d",strerror(errno),__LINE__);
perror("unable to take lock");
exit(1);
}

cout << "process thread process mutex acquired" << endl;

int lnVal = ExecuteFunction(lstData);
if(lnVal != 0)
{
exit(1);
}
}
}
}
</code>
Last edited on
It's [code][/code], not <code></code>

Like so.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void *ProcessThread(void *pArg)
{
  int lnReturnVal = 0;
  while (true) {
    if (!g_cProcessList.empty()) {
//pthread_mutex_unlock(&g_cProcessMutex);
      printf("In funtion \nthread id = %d\n", pthread_self());
      lnReturnVal = pthread_mutex_lock(&g_cProcessMutex);
      if (lnReturnVal != 0) {
        printf("%s, %d ", strerror(errno), __LINE__);
        perror("unable to take lock");
        exit(1);
      }
      cout << "process thread process mutex acquired" << endl;
      tagData lstData = g_cProcessList.back();
      g_cProcessList.pop_back();

      lnReturnVal = pthread_mutex_unlock(&g_cProcessMutex);
      if (lnReturnVal != 0) {
        printf("%s, %d", strerror(errno), __LINE__);
        perror("unable to take lock");
        exit(1);
      }

      cout << "process thread process mutex acquired" << endl;

      int lnVal = ExecuteFunction(lstData);
      if (lnVal != 0) {
        exit(1);
      }
    }
  }
}


> what would u suggest
Telling us about all the other places where g_cProcessList is manipulated.

Extending the lock to also cover g_cProcessList.empty() as well.

Running helgrind to help you find other places where you've messed up shared data with missing locks.
$ valgrind --tool=helgrind --ignore-thread-creation=yes ./a.out

Extending the lock to also cover g_cProcessList.empty() as well.

I think that's the problem here. Consider this case when the list has one item.
- Thread 1 sees that the list isn't empty and takes the lock.
- Thread 2 sees that the list isn't empty and waits to acquire the lock.
- Thread 1 removes the item from the list. Now it's emtpy.
- Thread 1 releases the lock
- Thread 2 takes the lock, thinking that the list is empty when in fact it is not.
Registered users can post here. Sign in or register to post.