SDL_PushEvent allocated memory?

I want to use the SDL_USEREVENT mechanism for passing messages between
threads. Although the method that I am using appears to work (at
least on Linux), it isn’t the “right” way to do things and I’m looking
for guidance.

I want to pass a message structure between threads. In my test
program, I am currently allocating memory in the first thread, sending
the event, and freeing the memory in the second thread, like so:

Thread 1:

    char * 	    p;
    const char *    str = "hello world" ;
    SDL_Event 	    event;
    
    if ((p = malloc(strlen(str) + 1)) == NULL)
        {
            SDL_Quit();
            exit(1);
        }
    strcpy(p, str);
    event.type = SDL_USEREVENT;
    event.user.code = 23;
    event.user.data1 = (void *) str;
    event.user.data2 = (void *) p;
    SDL_PushEvent(&event);

Thread 2:

	case SDL_USEREVENT:
	    printf("Got user event:\n"); fflush(stdout);
	    printf("  code = (%d)\n", event.user.code);
	    printf("  user1 = (%s)\n", (char *) event.user.data1);
	    printf("  user2 = (%s)\n", (char *) event.user.data2);
	    free(event.user.data2);
	    break;

However, depending on the implementation of the multi-threading
environment, there’s something missing here. If the threads are not
sharing data, then the allocated pointer would be passed across the
thread boundary and the data must be copied. Additionally, if the
threads are not sharing data, the malloc’ed pointer would need to be
freed by the event handler in thread 1 after allocating memory in
thread 2’s address space, copying the data to the newly-allocated
memory, and putting that memory into the Event element inserted onto
thread 2’s event queue.

Are we making the assumption in SDL that threads can always share
data regardless of environment, and that it’s ok to allocate memory in
one thread, pass the pointer across thread boundaries, and free that
allocated memory in a different thread?

I just want to make sure that I know what assumptions to work under.

Thanks!

Derrell

Are we making the assumption in SDL that threads can always share
data regardless of environment, and that it’s ok to allocate memory in
one thread, pass the pointer across thread boundaries, and free that
allocated memory in a different thread?

Yes, SDL will never fake threads with fork().
You might watch out for non-thread-safe memory manager implementations though.
I don’t know if it’s a problem on any system that advertises thread support,
but I know using SDL with libc 5 Linux systems might run into that problem.

See ya!
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

[…]

Are we making the assumption in SDL that threads can always share
data regardless of environment, and that it’s ok to allocate memory
in one thread, pass the pointer across thread boundaries, and free
that allocated memory in a different thread?

I can’t guarantee that there aren’t platforms breaking the rules, but
normally, threads share memory by definition, as opposed to
processes.

(Under Linux - thanks to a brilliant invention by Linus - you can
actually pick and chose what you want to share when creating a
thread. The same syscall is used by both threading APIs and
forking/process creation. However, that’s a different story, and it’s
Linux specific. It’s not exposed in the standard APIs.)

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Saturday 03 March 2001 22:21, Derrell Lipman wrote: