ps3dev / ps3toolchain

A script to autobuild an open source toolchain for the PS3.
BSD 2-Clause "Simplified" License
280 stars 92 forks source link

ppu-g++ std::thread problem #122

Open humbertodias opened 6 months ago

humbertodias commented 6 months ago

Is there anything missing on ppu-g++ to compile std::thread ?

Trying to compile std::thread

docker run -i --rm zeldin/ps3dev-docker bash -s <<EOF
echo '#include <iostream>
#include <thread>

// Function to be executed by the thread
void threadFunction(int threadID) {
    std::cout << "Thread " << threadID << " is executing" << std::endl;
}

int main() {
    // Creating a thread and passing a function to execute
    std::thread t1(threadFunction, 1);

    // Joining the thread to the main thread
    t1.join();

    // Output a message from the main thread
    std::cout << "Main thread is executing" << std::endl;

    return 0;
}' > oo.cpp
ppu-g++ oo.cpp -o oo -std=c++11
EOF

output

oo.cpp: In function 'int main()':
oo.cpp:11:10: error: 'thread' is not a member of 'std'
     std::thread t1(threadFunction, 1);
          ^~~~~~
oo.cpp:11:10: note: suggested alternative: 'tera'
     std::thread t1(threadFunction, 1);
          ^~~~~~
          tera
oo.cpp:14:5: error: 't1' was not declared in this scope
     t1.join();
     ^~
oo.cpp:14:5: note: suggested alternative: 'tm'
     t1.join();
     ^~
     tm
StrawFox64 commented 6 months ago

something similar to this here

humbertodias commented 5 months ago

It appears that ppu-g++ 13.2.0 lacks support for std::thread. Consequently, I opted for the traditional C pthread approach as an alternative inside cpp code using extern "C".

Instead of

std::thread t1(threadFunction, 1);

use

pthread_create(&thread_id, nullptr, thread_function, nullptr);

[!NOTE]
pthread isn't supported natively as well on ps3. however, luckelly we have a ported version of it here pthread-emb-ps3.

How I did for testing

docker run -i --rm hldtux/ps3dev bash -s <<EOF
echo '#include <iostream>
extern "C" {
#include <pthread.h>
}

void* thread_function(void* arg) {
    std::cout << "Hello from thread\n";
    return nullptr;
}

int main() {
    pthread_t thread_id;
    pthread_create(&thread_id, nullptr, thread_function, nullptr);
    pthread_join(thread_id, nullptr);
    std::cout << "Back in main thread\n";
    return 0;
}' > main.cpp
ppu-g++ main.cpp -o main \
-I/usr/local/ps3dev/portlibs/ppu/include \
-L/usr/local/ps3dev/portlibs/ppu/lib -lpthread
ppu-g++ --version
file main
EOF

output

ppu-g++ (GCC) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

main: ELF 64-bit MSB executable, 64-bit PowerPC or cisco 7500, Power ELF V1 ABI, version 1 (SYSV), statically linked, with debug_info, not strippe
d