Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-threading example is not multi-threading #57

Open
lucwens opened this issue Oct 11, 2023 · 2 comments
Open

Multi-threading example is not multi-threading #57

lucwens opened this issue Oct 11, 2023 · 2 comments
Labels
acknowledged the team is aware of this issue

Comments

@lucwens
Copy link

lucwens commented Oct 11, 2023

On Windows 10 and with julia 1.9.3-64 bit, the multi-threading examples from the documentation do not seem to multi-thread. The supposedly multi-threading tasks are executed in the main thread upon calling join(), they do not start before the sleep_for statement in main.

#include
#include <julia.h>
#include <jluna.hpp>
JULIA_DEFINE_FAST_TLS // only define this once, in an executable (not in a shared library) if you want fast code.

using namespace std;
using namespace jluna;

int main(int argc, char* argv[])
{
jluna::initialize(8);
std::vector<Task> tasks;
{
// declare lambda
std::function<void()> print_numbers = -> void
{
for (size_t i = 0; i < 10000; ++i)
if (i%100 == 0)
std::cout << i << std::endl;
};

	// add task to storage
	tasks.push_back(ThreadPool::create(print_numbers));

	// wait for 1ms
	std::this_thread::sleep_for(1ms);
}

for (auto& Task : tasks)
	Task.schedule();

// wait for another 10ms
std::this_thread::sleep_for(10000ms);
std::cout << "Main waited 10 sec" << std::endl;

for (auto& Task : tasks)
	Task.join();

return 0;

}

@Clemapfel
Copy link
Owner

Your code does not compile for me, after correcting the missing template argument in std::vector<Task> and the lambda capture clause of print_numbers, it runs for me as expected

jluna::initialize(8);
std::vector<Task<void>> tasks;
{
    // declare lambda
    std::function<void()> print_numbers = []() -> void
    {
        for (size_t i = 0; i < 10000; ++i)
            if (i%100 == 0)
                std::cout << i << std::endl;
    };

    // add task to storage
    tasks.push_back(ThreadPool::create(print_numbers));

    // wait for 1ms
    std::this_thread::sleep_for(1ms);
}

for (auto& Task : tasks)
    Task.schedule();

// wait for another 10ms
std::this_thread::sleep_for(10000ms);
std::cout << "Main waited 10 sec" << std::endl;

for (auto& Task : tasks)
    Task.join();
...
9700
9800
9900
Main waited 10 sec

I will try to test this on my windows 10 machine as well

@Clemapfel Clemapfel added the acknowledged the team is aware of this issue label Dec 4, 2023
@lucwens
Copy link
Author

lucwens commented Dec 5, 2023

I still get this result
[JULIA][LOG] initialization successful (1 thread(s)).
Main waited 10 sec
0
100
200
300
400
500
600
700
800
900

This is with Julia 9.1.4 in Visual Studio 2022 on Windows 10 and using the source from
your reply, so quite puzzled why we get a different result. Zip of project added for your reference
Julia2.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
acknowledged the team is aware of this issue
Projects
None yet
Development

No branches or pull requests

2 participants