bugSimple C Expat Wrapper - Bugs: bug #41001, multi-thread environment will crash

 
 

bug #41001: multi-thread environment will crash

Submitted by:  None
Submitted on:  Thu 26 Dec 2013 05:50:06 AM UTC  
 
Category: NoneSeverity: 3 - Normal
Item Group: NoneStatus: Fixed
Privacy: PublicAssigned to: Aleix Conchillo Flaqué <aleix>
Open/Closed: Closed

Add a New Comment(Rich Markup)
   

You are not logged in

Please log in, so followups can be emailed to you.

 

Mon 13 Jan 2014 08:53:46 PM UTC, comment #5:

I have pushed a fix to the git repo. Can you try it?

Aleix Conchillo Flaqué <aleix>
Project AdministratorIn charge of this item.
Mon 13 Jan 2014 08:38:41 PM UTC, comment #4:

You are right, there's an error in the code. using your example, it should be like this:

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>

static is_stop =0;
static pthread_key_t key_error_;
static pthread_once_t key_once_ = PTHREAD_ONCE_INIT;

static void create_keys_ (void)
{
int* error = NULL;

printf("create_keys_ thread %x\n", pthread_self());
pthread_key_create (&key_error_, free);

}

void error_code_init (int code)
{
int *error = NULL;

printf("error_code_init thread %x\n", pthread_self());

/* Initialize error code per thread. */
pthread_once (&key_once_, create_keys_);

error = (int *) pthread_getspecific (key_error_);
if (NULL == error)
{
error = (int *) malloc (sizeof (int));
pthread_setspecific (key_error_, error);
}
*error = code;
}

static void * th1_methd(void *arg)
{
printf("this is th1 thread %x\n", pthread_self());
sleep(10);
error_code_init(1);
is_stop++;
return NULL;
}

static void * th2_methd(void *arg)
{
printf("this is th2 thread %x\n", pthread_self());
sleep(2);
error_code_init(2);
is_stop++;
return NULL;
}

int main()
{
pthread_t th1;
pthread_t th2;

(void) pthread_create(&th1, NULL, th1_methd, 0);
(void) pthread_create(&th2, NULL, th2_methd, 0);

while(is_stop!= 2)
{
sleep(1);
}

printf("main exit\n");

return 0;
}

Aleix Conchillo Flaqué <aleix>
Project AdministratorIn charge of this item.
Sat 11 Jan 2014 03:12:14 AM UTC, comment #3:

I try to use "pthread_once" as yours , but it crash.
The code is as follow(It will crash).

gcc main.c -o testpthreadonce -lpthread -g

#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
static is_stop =0;
static pthread_key_t key_error_;
static pthread_once_t key_once_ = PTHREAD_ONCE_INIT;

static void create_keys_ (void)
{
int* error = NULL;

printf("create_keys_ thread %x \n", pthread_self());
pthread_key_create (&key_error_, free);

error = (int *) malloc (sizeof (int));
*error = -1;
pthread_setspecific (key_error_, error);
}

void error_code_init (int code)
{
int *error = NULL;

printf("error_code_init thread %x \n", pthread_self());
/* Initialize error code per thread. */
pthread_once (&key_once_, create_keys_);

error = (int *) pthread_getspecific (key_error_);
*error = code;
}

static void * th1_methd(void *arg)
{
printf("this is th1 thread %x \n", pthread_self());
sleep(10);
error_code_init(1);
is_stop++;
return NULL;
}

static void * th2_methd(void *arg)
{
printf("this is th2 thread %x \n", pthread_self());
sleep(2);
error_code_init(2);
is_stop++;
return NULL;
}

int main()
{

pthread_t th1;
pthread_t th2;

(void) pthread_create(&th1, NULL, th1_methd, 0);
(void) pthread_create(&th2, NULL, th2_methd, 0);

while(is_stop!= 2)
{
sleep(1);
}

printf("main exit");
}

Anonymous
Sat 11 Jan 2014 02:51:17 AM UTC, comment #2:

In linux, and the compiler is gcc.

Anonymous
Fri 03 Jan 2014 09:49:30 PM UTC, comment #1:

Can you give more details on the platform and compiler you are using?

Thanks.

Aleix Conchillo Flaqué <aleix>
Project AdministratorIn charge of this item.
Thu 26 Dec 2013 05:50:06 AM UTC, original submission:

the function is scew_error_set_last_error_
*error = code ; //this place will crash in multi-thread environment will crash for it did not initialized in all threads.

Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach File(s):
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -unavailable- added by aleix (Posted a comment)
  •  

    Do you think this task is very important?
    If so, you can click here to add your encouragement to it.
    This task has 0 encouragements so far.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

     

     

    Follow 4 latest changes.

    Date Changed By Updated Field Previous Value => Replaced By
    Thu 16 Jan 2014 05:49:48 AM UTCaleixOpen/ClosedOpen=>Closed
    Mon 13 Jan 2014 08:53:46 PM UTCaleixStatusNone=>Fixed
    Fri 03 Jan 2014 09:49:42 PM UTCaleixAssigned toNone=>aleix
    Fri 03 Jan 2014 09:49:30 PM UTCaleixPrivacyPrivate=>Public

    Back to the top


    Powered by Savane 3.1-cleanup1