[Spread-users] spread 4.0rc2, C and multithreading

jabe at gfz-potsdam.de jabe at gfz-potsdam.de
Fri Oct 27 02:10:56 EDT 2006

I try to use spread in a multithreaded environment.
The main thread does all the connection handling while the listener thread 
reads incoming messages.
When I disconnect from the main thread, the listener thread receives an error 
(connection closed by spread) and terminates. That behavior is intended.

When I connect and disconnect, everything works as expected.
After connecting again, the listener thread will neither receive a message nor 
an error from spread anymore. The thread block in SP_receive and even if I 
call disconnect from the main thread, SP_receive does not receive the 
"connection closes by spread" error.

Do I miss something important?

Below I posted a minimal example of this problem.

System: Linux SuSE 10.1, 32bit, gcc 4.1, spread 4.0rc2


------- source --------

#include <stdio.h>
#include <pthread.h>
#include "sp.h"

static mailbox mbox;
static char privateGroup[MAX_GROUP_NAME];
static pthread_t thread;

void* listen(void*) {
        service serviceType = 0;
        char sender[MAX_GROUP_NAME];
        int numGroups;
        char groups[10][MAX_GROUP_NAME];
        int16 msgType;
        int endianMissmatch;
        char message[1024];

        printf("[thread] starting listener thread\n");

        while ( true ) {
                int ret = SP_receive(mbox, &serviceType, sender, 10, 
&numGroups, groups,
                                     &msgType, &endianMissmatch, 1024, 
                if ( ret < 0 ) {
                        printf("[thread] ");

                printf("[thread] received a message\n");

        printf("[thread] listener thread finished\n");


void connect() {
        mbox = -1;
        int ret = SP_connect("4803 at localhost", "testclient", 0, 0, &mbox, 
        if ( ret != ACCEPT_SESSION ) {

        printf("[main] connected successfully\n");

        pthread_create(&thread, NULL, listen, NULL);

void disconnect() {
        int ret = SP_disconnect(mbox);
        if ( ret < 0 )
                SP_error( ret );
        printf("[main] disconnected\n");

        printf("[main] waiting for listener thread\n");
        pthread_join(thread, NULL);

int main(int argc, char *argv[]) {
        disconnect(); // this call does not terminate

        return 0;

More information about the Spread-users mailing list