Take the 2-minute tour ×
typedef struct client
   pthread thread;
   Window_t *win

client * client_create(int ID)
    client *new_Client = (client *) malloc(sizeof(client));
    char title[16];

    if (!new_Client) return NULL;

    sprintf(title, "Client %d", ID);

    /* Creates a window and set up a communication channel with it */
    if ((new_Client->win = window_create(title))) 
        return new_Client;
    else {
        return NULL;

When the user inputs 'e' I try to create a new thread with a new client and window by doing this is my int_main. The flag is just to tell me that the user entered e

if(eflag == 1)
    client *c = NULL;
    pthread_create(&c.thread, NULL, client_create, (void *) &started);
    eflag =0;

This is supposed to create a new client on a new thread on a new window but it doesn't do that.

I'm not sure what to put in my pthread_create, and also how am I supposed get a new instance of client, because the client_create function creates a new window. And when I try to create a new thread by doing pthread_create it also creates a new window... If this was java oeverytime the user pressed 'e' I would just create a new instance of the class client... but I can't really do that here. any suggestions?

share|improve this question
What does your code do? What is this window_create function? –  hyde Feb 22 '13 at 5:06
initially this is a single-threaded program. the window_createe crreates a new window in which the user can interact with a database, . i'm trying to make it, so you can have as many users as you want –  user1665569 Feb 22 '13 at 5:14

1 Answer 1

up vote 1 down vote accepted

The prototype of the pthread_create function is

int pthread_create(pthread_t *restrict thread,
              const pthread_attr_t *restrict attr,

your start_routine definition needs to match void *(*start_routine)(void*).. And it is executed in a new thread context.

void *my_client_routine(void *arg) {
    while(1) {
        // do what each client does.

Currently in your main() you are executing your create_client function twice. once before the call to pthread_create() and once as part of the new thread spawned from pthread_create. What you probably want to do is

  c = create_client()
  pthread_create(&c.thread, NULL, my_client_routine, &args_that_client_routine_needs);
share|improve this answer

Your Answer


